home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection 1998 Fall: Game Toolkit / Disc.iso / Samples / Moofwars 1.02 / MoofWars Sprocket / •Headers / TCommandTimer.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-10  |  3.1 KB  |  94 lines  |  [TEXT/CWIE]

  1. /*************************************************************************************
  2. TCommandTimer.h
  3.  
  4. TCommandTimer is used to provide the game with a clock that periodically samples the
  5. input device -- currently, just the keyboard.  The inputs beind received can be used
  6. to synchronize the game action.
  7.  
  8. This code implements a time manager task that periodically grabs the state of the
  9. keyboard.  It stores these in a buffer where the main application task can retrieve it.
  10.  
  11. We create a buffer for two seconds worth of commands, although in practice we'll almost
  12. never need more than about a 1/10 of a second.
  13.  
  14. Eventually, this code will also probably need to deal with networking tasks.
  15.  
  16. Author: Timothy Carroll
  17. Apple Developer Technical Support
  18. timc@apple.com
  19.  
  20. Modification History: 
  21.  
  22. 8/7/98        TMC        Switched from VBL to timer task and completely overhauled the interface.
  23. 8/15/96        TMC     Initial Release    
  24. 1/23/96     TMC        Added include for Moofwars.h so that MrC will compile     
  25.  
  26. Copyright © 1996, 1997, 1998 Apple Computer, Inc., All Rights Reserved
  27.  
  28. You may incorporate this sample code into your applications without
  29. restriction, though the sample code has been provided "AS IS" and the
  30. responsibility for its operation is 100% yours.  However, what you are
  31. not permitted to do is to redistribute the source as "DSC Sample Code"
  32. after having made changes. If you're going to re-distribute the source,
  33. we require that you make it clear in the source that the code was
  34. descended from Apple Sample Code, but that you've made changes.
  35. *************************************************************************************/
  36. #ifndef _TCOMMANDTIMER_
  37. #define _TCOMMANDTIMER_
  38.  
  39. #pragma once
  40.  
  41. #include <Events.h>
  42.  
  43. #if PRAGMA_STRUCT_ALIGN
  44. #pragma options align=power
  45. #endif
  46.  
  47. // TInputState holds all of the data for one "frame" of the game.  For the moment,
  48. // we're just sending back a copy of the KeyMap, but in the future we might grab
  49. // the state of the mouse, network events, and so on.
  50.  
  51. struct TInputState
  52. {
  53.     KeyMap        keys;
  54. };
  55.  
  56. // All of the data for the command object is stored in a separate struct.
  57. struct CommandTimerData;
  58.  
  59. class TCommandTimer
  60. {
  61.     public:
  62.         TCommandTimer (void);
  63.         ~TCommandTimer (void);
  64.         
  65.     // Call this routine with the number of frames per second that the inputs should
  66.     // be sampled.  If you pass in 0, then no timer or buffer is created. IsCommandWaiting
  67.     // will always return true, and RetrieveCommand and PeekCommand just sample the inputs.
  68.         OSStatus Initialize (SInt16 framesDesired);
  69.         
  70.         // If IsAcceptingCommands is true, the timer is running.  Some functions can
  71.         // only be called if the timer is stopped.
  72.         Boolean IsAcceptingCommands (void);
  73.         void AcceptCommands (Boolean commands);
  74.         
  75.         // Only call this when the timer is stopped.
  76.         void FlushCommands(void);
  77.         
  78.         // PeekCommand does not remove the command from the buffer.  Use sparingly, to
  79.         // prevent buffer overflows.
  80.         Boolean IsCommandWaiting (void);
  81.         Boolean RetrieveCommand (TInputState *command);
  82.         Boolean PeekCommand (TInputState *command);
  83.     private:
  84.         CommandTimerData *timerData;
  85.     
  86. };
  87.  
  88. #if PRAGMA_STRUCT_ALIGN
  89. #pragma options align=reset
  90. #endif
  91.  
  92.  
  93. #endif /* _TCOMMANDTIMER_ */
  94.